import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CollapsePanel {
  final String title;
  final Widget content;
  final bool isExpanded;
  bool isSelected;
  final void Function(bool)? onSelectedChanged;

  CollapsePanel({
    required this.title,
    required this.content,
    this.isExpanded = false,
    this.isSelected = false,
    this.onSelectedChanged,
  });
}

class CollapseController extends GetxController {
  final List<CollapsePanel> panels;
  final bool allowMultiple;
  final ValueChanged<List<bool>>? onExpansionChanged;
  var isExpanded = <bool>[].obs;

  CollapseController({
    required this.panels,
    this.allowMultiple = false,
    required this.onExpansionChanged,
  }) {
    /*isExpanded.value = List.generate(panels.length, (index) => panels[index].isExpanded);*/
    isExpanded.value = panels.map((panel) => panel.isExpanded).toList();
  }

  void handleTap(int index) {
    if (allowMultiple) {
      // 允许多面板同时展开
      isExpanded[index] = !isExpanded[index];
    } else {
      // 仅允许单面板展开
      for (var i = 0; i < panels.length; i++) {
        if (i == index) {
          isExpanded[i] = !isExpanded[i]; // 切换当前面板状态
        } else {
          isExpanded[i] = false; // 关闭其他所有面板
        }
      }
    }
    isExpanded.refresh();
    onExpansionChanged?.call(isExpanded);
  }

  void handleSelectedChange(int index, bool value) {
    panels[index].onSelectedChanged?.call(value);
  }
}

class Collapse extends StatelessWidget {
  final List<CollapsePanel> panels;
  final bool allowMultiple;
  final ValueChanged<List<bool>>? onExpansionChanged;
  final bool showArrow;

  Collapse({
    super.key,
    required this.panels,
    this.allowMultiple = false,
    this.onExpansionChanged,
    this.showArrow = true,
  });

  late final controller = Get.put(
    CollapseController(
      panels: panels,
      allowMultiple: allowMultiple,
      onExpansionChanged: onExpansionChanged,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Column(
      children: List.generate(
        panels.length,
        (index) => Card(
          color: Colors.white,
          margin: const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.zero,
            side: BorderSide.none,
          ),
          elevation: 0,
          child: Container(
            decoration: BoxDecoration(
              border: Border(
                top: BorderSide(color: Color(0xFFD3D3D3), width: 1),
              ),
            ),
            child: Column(
              children: [
                InkWell(
                  onTap: () => controller.handleTap(index),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: 4,
                      horizontal: 8,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Expanded(
                          child: Text(
                            panels[index].title,
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        if (panels[index].onSelectedChanged != null)
                          Transform.scale(
                            scale: 0.7,
                            child: Switch(
                              activeColor: Colors.blue,
                              materialTapTargetSize:
                                  MaterialTapTargetSize.shrinkWrap,
                              value: panels[index].isSelected,
                              onChanged:
                                  (value) => controller.handleSelectedChange(
                                    index,
                                    value,
                                  ),
                            ),
                          ),
                        if (showArrow)
                          Obx(
                            () => Icon(
                              controller.isExpanded[index]
                                  ? Icons.keyboard_arrow_up
                                  : Icons.keyboard_arrow_down,
                            ),
                          ),
                      ],
                    ),
                  ),
                ),
                Obx(
                  () => AnimatedCrossFade(
                    firstChild: SizedBox.shrink(),
                    secondChild: panels[index].content,
                    crossFadeState:
                        controller.isExpanded[index]
                            ? CrossFadeState.showSecond
                            : CrossFadeState.showFirst,
                    duration: const Duration(milliseconds: 200),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
